package com.fr71.project.dm.utils;

import com.fr71.common.constant.CacheConstants;
import com.fr71.common.utils.spring.SpringUtils;
import com.fr71.framework.redis.RedisCache;
import com.fr71.project.dm.domain.Beans;
import com.fr71.project.dm.domain.QQTryCodeBean;
import com.fr71.project.dm.listener.RedisKeyExpirationListener;

import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;

public class RedisUtil {

    private static RedisCache redisCache;

    private static RedisCache getRedisCache() {
        if (redisCache == null) {
            redisCache = SpringUtils.getBean(RedisCache.class);
        }
        return redisCache;
    }

    //QQ签到key
    public static final String QQ_SIGN_KEY = "sign_data:";

    //换绑标记，后面跟ip  里面存换绑次数 key有效期1分钟
    public static final String CHANGE_TIME = "change_log:";

    //qq领取卡的标记，每周一清空 后面跟qq
    public static final String MARK_TRY_CODE_MARK_HEAD = "mark_code_head:";

    //qq领取卡的临时数据 尝试领取时写入 5分钟有效，领取后删除
    public static final String TRY_CODE_TMP = "try_code_head:";


    /**
     * 获取key可以领取的激活码
     */
    public static QQTryCodeBean getTryCodeTypeByKey(Long qq, String key) {
        return getRedisCache().getCacheObject(TRY_CODE_TMP + qq + "_" + key);
    }

    /**
     * 获取key可以领取的激活码
     */
    public static String getKeyByqq(Long qq) {
        try {
            for (String key : getRedisCache().keys(TRY_CODE_TMP + qq + "*")) {
                String u = key.split(TRY_CODE_TMP + qq)[1];
                return u.substring(1, u.length());
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void delAllQQMark() {
        getRedisCache().deleteObject(getRedisCache().keys(TRY_CODE_TMP + "*"));
        getRedisCache().deleteObject(getRedisCache().keys(MARK_TRY_CODE_MARK_HEAD + "*"));
    }

    public static void delTryCodeTypeMark(Long qq, String key) {
        //标记这周已经领取了
        getRedisCache().setCacheObject(MARK_TRY_CODE_MARK_HEAD + qq, 0);
        //删除key
        getRedisCache().deleteObject(TRY_CODE_TMP + qq + "_" + key);
    }

    /**
     * 写入qq号请求体验卡的数据
     * [qq]申请的qq
     * [uType] 用户类型
     */
    public static String insertAnTryCodeMark(Long qq, Integer uType, Integer addTimes) {
        String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        getRedisCache().setCacheObject(TRY_CODE_TMP + qq + "_" + uuid, new QQTryCodeBean(qq, uType, addTimes), 5, TimeUnit.MINUTES);
        return uuid;

//        it.updateTime = DateUtils.getNowDate();
//        it.createTime = it.updateTime;
//        it.bindIp = order.bindIp;
//        it.authStartTime = DateUtils.getNowDate();
//        it.authEndTime = endTime;
//        it.setsCode(order.getsCode());
//        it.setuType(order.orderType);
//        it.authType = 0;
//        it.changeTimesMonth = 0;
//        it.version = "v1.0.0";
//        if (!order.qq.isNullOrEmpty()) {
//            it.bindQq = order.qq;
//        }
    }

    /**
     * 查询这个qq是否可以领取本周的激活码
     * [qq]
     * @return 返回true可以领取 false不可领取
     */
    public static boolean canGetTryCode(Long qq) {
        return getRedisCache().getCacheObject(MARK_TRY_CODE_MARK_HEAD + qq) == null;
    }

    /**
     * 获取总尝试换绑次数
     */
    public static int getTryTimes(String ip) {
        Integer result = getRedisCache().getCacheObject(CHANGE_TIME + ip);
        return result != null ? result : 0;
    }

    /**
     * 尝试次数增加，刷新时间
     */
    public static void addTryTimes(String ip) {
        getRedisCache().setCacheObject(CHANGE_TIME + ip, getTryTimes(ip) + 1, 10, TimeUnit.MINUTES);
    }

    /**
     * 获取Q签到数据
     */
    public static Beans.QQSignBean getQQSignData(String qq) {
        Beans.QQSignBean result = getRedisCache().getCacheObject(QQ_SIGN_KEY + qq);
        return result != null ? result : new Beans.QQSignBean(qq, 0, 0, 0, 0);
    }

    /**
     * 设置新的签到数据
     */
    public static void setQQSignData(String qq, Object qqSignBean) {
        getRedisCache().setCacheObject(QQ_SIGN_KEY + qq, qqSignBean);
    }


    /**
     * 打一个标记，用订单号
     */
    public static void markOrder(String orderId) {
        getRedisCache().setCacheObject(
            RedisKeyExpirationListener.KEY_ORDER_EXPIRATION + orderId,
            "",
            3,
            TimeUnit.MINUTES
        );
    }

    /**
     * 标记当月已领取体验卡
     */
    public static void markGetTryTimes(String code) {
        getRedisCache().setCacheObject(CacheConstants.TRY_CODE_TIMES + code, 1);
    }

    /**
     * 获取领取次数
     */
    public static int canGetTryCodeTimes(String code) {
        Integer result = getRedisCache().getCacheObject(CacheConstants.TRY_CODE_TIMES + code);
        return result != null ? result : 0;
    }

    public static void clearTryCodeTimes() {
        getRedisCache().deleteObject(getRedisCache().keys(CacheConstants.TRY_CODE_TIMES + "*"));
    }

    /**
     * 删除过期的标记
     */
    public static void delOrderMark(String orderId) {
        //删除过期的标记
        getRedisCache().deleteObject(RedisKeyExpirationListener.KEY_ORDER_EXPIRATION + orderId);
    }

    /**
     * 获取版本信息
     */
    @SuppressWarnings("unchecked")
    public static List<HashMap<String, Object>> getVersionList() {
        List<HashMap<String, Object>> result = getRedisCache().getCacheList("soft:version_infos");
        return result != null ? result : new ArrayList<>();
    }

    /**
     * 设置版本信息
     */
    public static void setVersionInfos(List<HashMap<String, Object>> list) {
        getRedisCache().deleteObject("soft:version_infos");
        getRedisCache().setCacheList("soft:version_infos", list);
    }

    /**
     * 获取公告列表
     */
    @SuppressWarnings("unchecked")
    public static List<HashMap<String, Object>> getNoticeList() {
        List<HashMap<String, Object>> result = getRedisCache().getCacheList("soft:notice");
        return result != null ? result : new ArrayList<>();
    }

    /**
     * 更新公告
     */
    public static void setNoticesList(List<HashMap<String, Object>> list) {
        getRedisCache().deleteObject("soft:notice");
        getRedisCache().setCacheList("soft:notice", list);
    }
}